package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.Member;
import com.jiuwu.spring.eshop.db.entity.VMember;
import com.jiuwu.spring.eshop.respository.MemberRepository;
import com.jiuwu.spring.eshop.respository.OrderRepository;
import com.jiuwu.spring.eshop.respository.VMemberRepository;
import com.jiuwu.spring.eshop.respository.VOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@Controller
public class IndexAccount {


    @Autowired
    @Lazy
    VMemberRepository vMemberRepository;


    @Autowired
    MemberRepository memberRepository;

    @Autowired
    OrderRepository orderRepository;


    @Autowired
    VOrderRepository vOrderRepository;

    @RequestMapping("/account")
    public String account(@RequestParam(value = "id",required = false,defaultValue = "2") Integer id,
                          Model model, HttpSession session){
        String userName = (String) session.getAttribute("userName");
        model.addAttribute("userName",userName);
        model.addAttribute("id",id);
        if(!StringUtils.isEmpty(userName)){
            Optional<VMember> byNameEquals = vMemberRepository.findByNameEquals(userName);
            byNameEquals.ifPresent(member -> model.addAttribute("vMember",member));
        }else{
            model.addAttribute("vMember",null);
        }

        switch (id){
            case 2:
                model.addAttribute("orderList",orderRepository.findAllByOrderNameEquals(userName));
                break;
            case 3:
                if(!StringUtils.isEmpty(userName)){
                    memberRepository.findByMemberNameEquals(userName)
                            .ifPresent(member -> model.addAttribute("member",member));
                }

                break;
            case 5:

                break;



        }

        return "/account";
    }


}
